package com.bt.lms.service.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.Role;
import com.bt.lms.entity.page.Page;
import com.bt.lms.dao.servise.RoleDao;
import com.bt.lms.dao.servise.impl.RoleDaoImpl;
import com.bt.lms.service.RoleService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 角色权限服务
 *
 * @author 徐大永
 */
public class RoleServiceImpl implements RoleService {

    @Override
    public Page<Role> pagingQuery(Page<Role> page) {

        //sql 角色信息
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("SELECT * FROM role WHERE deleted = ").append(BOConstant.DELETED_FALSE).append(" LIMIT ?,?");
        String sql = new String(sqlSB);
        //sql 数据总数
        StringBuilder roleSumSB = new StringBuilder();
        roleSumSB.append("SELECT COUNT(id) FROM role WHERE deleted=").append(BOConstant.DELETED_FALSE);
        String roleSum = new String(roleSumSB);
        //dao方法
        RoleDao roleDao = new RoleDaoImpl();
        return roleDao.select(sql, page,roleSum);

    }

    @Override
    public Page<Role> corRoleQuery(Page<Role> page) {

        //sql 角色信息
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("SELECT id,code,name,details FROM role WHERE deleted = ").append(BOConstant.DELETED_FALSE).append(" LIMIT ?,?");
        String sql = new String(sqlSB);
        //sql 数据总数
        StringBuilder roleSumSB = new StringBuilder();
        roleSumSB.append("SELECT COUNT(id) FROM role WHERE deleted=").append(BOConstant.DELETED_FALSE);
        String roleSum = new String(roleSumSB);
        //dao方法
        RoleDao roleDao = new RoleDaoImpl();
        return roleDao.selectCor(sql, page,roleSum);
    }

    @Override
    public boolean add(Role role) {

        //关于表，和几张表有关系 role(本表
        //1.需要系统维护那些属性（不需要用户输入的属性）
        UUID uuid = UUID.randomUUID();
        role.setCode(uuid.toString());//角色编码
        role.setBuildTime(LocalDateTime.now());//创建时间
        role.setStatus(BOConstant.ROLE_STATUS_START);//状态(默认启用
        role.setBuilder("张三");//创建人
        //2.准备动数据库  准备SQL
        String roelSql = "INSERT INTO role (code,name,details,builder,builder_time,status) VALUES (?,?,?,?,?,?)";
        //2.1 调用dao 得角色id
        RoleDao roleDao = new RoleDaoImpl();
        int insert = roleDao.insert(roelSql, role);

        return insert == 1;

    }

    @Override
    public boolean edit(Role role) {
        //只有本表，且只有名称与描述
        //SQL
        String sql = "UPDATE role SET name=?,details=? WHERE id = ?";
        //dao
        RoleDao roleDao = new RoleDaoImpl();
        int i = roleDao.update(sql,role);
        return i == 1;
    }

    @Override
    public boolean del(Long id) {

        //dao对象
        RoleDao roleDao = new RoleDaoImpl();

        //两张表  本表+关系表   本表
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("UPDATE role SET deleted = ").append(BOConstant.DELETED_TRUE).append(" WHERE id = ?");
        String sql = new String(sqlSB);
        //dao方法
        int update = roleDao.update(sql, id);
        if (update==1){
            //关系表
            StringBuilder sqlRlSB = new StringBuilder();
            sqlRlSB.append("UPDATE rl_user_role SET deleted = ").append(BOConstant.DELETED_TRUE).append(" WHERE role_id = ?");
            String sqlRl = new String(sqlRlSB);
            //dao方法
            roleDao.update(sqlRl, id);
        }else
            return false;

        return true;
    }

    @Override
    public boolean corRole(Long userId, List<Long> roleIds) {

        //sql
        String sql = "INSERT INTO rl_user_role(user_id_,role_id,install_role_time) VALUES (?,?,?)";

        //dao 方法
        RoleDao roleDao = new RoleDaoImpl();
        int i = roleDao.inserts(sql,roleIds,userId);

        return i == roleIds.size();

    }

    @Override
    public boolean callOffCor(Long userId, Long roleId) {

        //sql
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("UPDATE rl_user_role SET deleted = ").append(BOConstant.DELETED_TRUE)
                .append(" WHERE user_id_=? and role_id=?");
        String sql =  new String(sqlSB);

        //调用dao
        RoleDao roleDao = new RoleDaoImpl();
        int i = roleDao.update(sql,userId,roleId);

        return i==1;
    }

    @Override
    public List<Long> queryRoleFromUser(Long userId) {

        //sql
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("SELECT role_id FROM rl_user_role WHERE is_delete=").append(BOConstant.DELETED_FALSE).append(" AND user_id_=?");
        String sql = new String(sqlSB);

        //dao 方法
        RoleDao roleDao = new RoleDaoImpl();

        return roleDao.select(sql,userId);
    }
}
